Skip to content
This repository has been archived by the owner on Sep 1, 2020. It is now read-only.

Latest commit

 

History

History
46 lines (39 loc) · 758 Bytes

3.14.5 - 异常处理.md

File metadata and controls

46 lines (39 loc) · 758 Bytes

异常处理

在协程编程中可直接使用try/catch处理异常。但必须在协程内捕获,不得跨协程捕获异常

错误

下面的代码中,try/catchthrow在不同的协程中,协程内无法捕获到此异常。当协程退出时,发现有未捕获的异常,将引起致命错误。

Fatal error: Uncaught RuntimeException
try
{
	Swoole\Coroutine::create(function () {
		throw new \RuntimeException(__FILE__, __LINE__);
	});
}
catch (\Throwable $e)
{
	echo $e;
}

正确

在协程内捕获异常。

function test()
{
	throw new \RuntimeException(__FILE__, __LINE__);
}

Swoole\Coroutine::create(function () {
	try
	{
		test();
	}
	catch (\Throwable $e)
	{
		echo $e;
	}
});